package com.ilop.sthome.common.command;

import android.content.Context;
import android.util.Log;

import com.aliyun.alink.linksdk.tmp.device.panel.PanelDevice;
import com.ilop.sthome.common.udp.GatewayUdpUtil;
import com.ilop.sthome.common.udp.UdpCommunicationUtil;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;

/**
 * @author skygge
 * @Date on 2020-02-19.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：发送其他相关指令（如网关、子设备报警信息）
 */
public class SendOtherCommand {
    private static final String TAG = SendOtherCommand.class.getSimpleName();
    private Context mContext;
    private SiterCommand mSend;
    private String mIotId;
    private String mDeviceName;

    public SendOtherCommand(Context context, DeviceInfoBean device) {
        this.mContext = context;
        this.mIotId = device.getIotId();
        this.mDeviceName = device.getDeviceName();
        mSend = new SiterCommand(device);
    }

    /**
     * send code
     * @param groupCode
     */
    private void sendAction(String groupCode){
        if (GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            Log.i(TAG, "sendCodeUDP: " + groupCode);
            new UdpCommunicationUtil(mDeviceName, groupCode);
        }else {
            Log.i(TAG, "sendCodeTCP: " + groupCode);
            PanelDevice panelDevice = new PanelDevice(mIotId);
            panelDevice.init(mContext, null);
            panelDevice.invokeService(groupCode, (b, o) -> {});
        }
    }

    /**
     * 报警推送信息同步请求
     */
    public void syncAlarms(int page){
        sendAction(mSend.syncAlarmLogs(page));
    }

    /**
     * 报警推送信息删除
     */
    public void deleteGatewayAlarms(int id){
        sendAction(mSend.deleteGatewayLog(id));
    }


    /**
     * 子设备信息同步请求
     */
    public void syncSubAlarms(final int page,final int eqid){
        sendAction(mSend.syncSubAlarmLogs(page,eqid));
    }

    /**
     * 子设备信息删除
     */
    public void deleteSubAlarms(final int id,final int eqid){
        sendAction(mSend.deleteSubDeviceLog(id,eqid));
    }
}
